package com.example.zuoye.controller;


import com.example.zuoye.entity.Book;
import com.example.zuoye.entity.User;
import com.example.zuoye.service.UserService;
import com.example.zuoye.service.BookService;
import lombok.extern.java.Log;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    User user = new User();

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/home")
    public String index(Model model){
        List<Book> books = bookService.selectBookInfo();
        List booksInfo = new ArrayList();
        List bookInfo = new ArrayList();
        for(int i = 0;i < books.size();i++){
            List bookTarget = new ArrayList();
            bookTarget.add(books.get(i).getBook_name());
            bookTarget.add(books.get(i).getBook_press());
            bookTarget.add(books.get(i).getBook_author());
            bookTarget.add(books.get(i).getBook_date());
            bookTarget.add(books.get(i).getBook_number());
            bookTarget.add(books.get(i).getBook_image());
            bookInfo.add(bookTarget);
        }
        booksInfo.add(bookInfo);
        model.addAttribute("booksInfo",bookInfo);
        return "home";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/ulogin")
    public String login(HttpServletRequest request,
                        HttpSession session,
                        Model model) throws Exception{

        System.out.println("执行了ulogin方法");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 通过用户名查询数据库，判断用户是否存在
        // 不存在跳转至注册页面
        String usernameDB = userService.findUserNameByUserName(username);
        if (usernameDB == null){
            System.out.println("该用户名不存在");
            model.addAttribute("tip_registered","该用户名不存在");
        }

        // 查询用户信息
        user = userService.checkByUP(username, password);
        if (user != null){
            // 若user存在写入session
            session.setAttribute("user_login",user);
            return "redirect:home";
        }else{
            model.addAttribute("tip_registered","密码不正确");
        }

        return "redirect:/home";
    }

    @RequestMapping("/uregister")
    public ModelAndView register(HttpServletRequest request,
                                 User user) throws Exception{
        System.out.println("执行了uregister方法");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("home");
        user.setUsername(user.getUsername());
        user.setPassword(user.getPassword());
        user.setPhone(user.getPhone());
        if (user.getUsername() == null || user.getPassword() == null || user.getPhone() == null) {
            mv.addObject("tip_registered","注册信息不能为空");
            return mv;
//            throw new Exception("注册信息不能为空");
        }

        // 对两次输入的密码进行校验
        if (!password.equals(password2)){
            mv.addObject("tip_registered","两次输入的密码不一致");
            return mv;
//            throw new Exception("两次输入的密码不一致");
        }
        // 数据库中查询username是否存在,不存在则插入,存在则提示:该用户名已经存在
        String usernameDB = userService.findUserNameByUserName(username);
        if (usernameDB == null){
            userService.insertUser(user);
        }else{
            mv.addObject("tip_registered","该用户名已注册");
        }
        return mv;
    }
}
